package com.cjh.wechat.core.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cjh.wechat.commom.pojo.Notify;
import com.cjh.wechat.commom.pojo.NotifyGroup;
import com.cjh.wechat.commom.pojo.UserAndGroup;
import com.cjh.wechat.commom.type.ApplyStatus;
import com.cjh.wechat.commom.type.GroupMemberLevel;
import com.cjh.wechat.commom.type.ReadStatus;
import com.cjh.wechat.commom.util.BasicUtil;
import com.cjh.wechat.core.dao.NotifyDao;
import com.cjh.wechat.core.dao.NotifyGroupDao;
import com.cjh.wechat.core.service.NotifyGroupService;
import com.cjh.wechat.core.service.UserAndGroupService;

@Service
public class NotifyGroupServiceImpl implements NotifyGroupService
{
    @Resource
    private NotifyGroupDao      notifyGroupDao;
    @Resource
    private NotifyDao           notifyDao;
    @Resource
    private UserAndGroupService userAndGroupService;
    
    @Override
    public void insertSelective(NotifyGroup notifyGroup)
    {
        notifyGroupDao.insertSelective(notifyGroup);
    }
    
    @Override
    public List<NotifyGroup> findByGid(String gid, int pageNum, int pageSize)
    {
        int startNum = (pageNum - 1) * pageSize;
        int endNum = (pageNum - 1) * pageSize + pageSize - 1;
        return notifyGroupDao.findByGid(gid, startNum, endNum);
    }
    
    @Override
    public int findByGidSum(String gid)
    {
        return notifyGroupDao.findByGidSum(gid);
    }
    
    @Override
    public boolean isExist(String gid, String fromUid, Integer type)
    {
        int num = notifyGroupDao.isExist(gid, fromUid, type);
        return num != 0 ? true : false;
    }
    
    @Override
    public void apply(NotifyGroup notifyGroup) throws Exception
    {
        notifyGroup.setDateTime(BasicUtil.getDateTime());
        notifyGroup.setIsRead(ReadStatus.UNREAD.value);
        notifyGroup.setContent("申请加入该群");
        notifyGroup.setType(ApplyStatus.INIT.value);
        notifyGroupDao.insertSelective(notifyGroup);
    }
    
    @Override
    @Transactional
    public void agreeApply(Integer no) throws Exception
    {
        // 状态修改
        NotifyGroup notifyGroup = notifyGroupDao.selectByPrimaryKey(no);
        notifyGroup.setType(ApplyStatus.AGREE.value);
        notifyGroupDao.updateByPrimaryKeySelective(notifyGroup);
        // 通知对方
        Notify notify = new Notify();
        notify.setContent("群组" + notifyGroup.getGid() + "同意了您的入群申请");
        notify.setDateTime(BasicUtil.getDateTime());
        notify.setFromUid(notifyGroup.getFromUid());
        notify.setIsRead(ReadStatus.UNREAD.value);
        notify.setUid(notifyGroup.getFromUid());
        notifyDao.insertSelective(notify);
        NotifyGroup notifyGroup2 = new NotifyGroup();
        notifyGroup2.setContent(notifyGroup.getFromUid() + "已加入该群");
        notifyGroup2.setGid(notifyGroup.getGid());
        notifyGroup2.setFromUid(notifyGroup.getFromUid());
        notifyGroup2.setIsRead(ReadStatus.UNREAD.value);
        notifyGroup2.setDateTime(BasicUtil.getDateTime());
        notifyGroup2.setType(ApplyStatus.SYS.value);
        notifyGroupDao.insertSelective(notifyGroup2);
        // 群员添加
        UserAndGroup userAndGroup = new UserAndGroup();
        userAndGroup.setGid(notifyGroup.getGid());
        userAndGroup.setNo(no);
        userAndGroup.setType(GroupMemberLevel.USER.value);
        userAndGroup.setUid(notifyGroup.getFromUid());
        userAndGroupService.insert(userAndGroup);
    }
    
    @Override
    @Transactional
    public void refuseApply(Integer no) throws Exception
    {
        NotifyGroup notifyGroup = notifyGroupDao.selectByPrimaryKey(no);
        notifyGroup.setType(ApplyStatus.REFUSE.value);
        notifyGroupDao.updateByPrimaryKeySelective(notifyGroup);
        Notify notify = new Notify();
        notify.setContent("群组" + notifyGroup.getGid() + "拒绝了您的入群申请");
        notify.setDateTime(BasicUtil.getDateTime());
        notify.setFromUid(notifyGroup.getFromUid());
        notify.setIsRead(ReadStatus.UNREAD.value);
        notify.setUid(notifyGroup.getFromUid());
        notifyDao.insertSelective(notify);
        NotifyGroup notifyGroup2 = new NotifyGroup();
        notifyGroup2.setContent(notifyGroup.getFromUid() + "被拒绝入群");
        notifyGroup2.setGid(notifyGroup.getGid());
        notifyGroup2.setFromUid(notifyGroup.getFromUid());
        notifyGroup2.setIsRead(ReadStatus.UNREAD.value);
        notifyGroup2.setDateTime(BasicUtil.getDateTime());
        notifyGroup2.setType(ApplyStatus.SYS.value);
        notifyGroupDao.insertSelective(notifyGroup);
    }
    
    @Override
    public void updateByGidSelective(NotifyGroup notifyGroup)
    {
        notifyGroupDao.updateByGidSelective(notifyGroup);
    }
    
}
